//-----------------------------------------------------------------------
// <copyright file="ProductCatDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class ProductCatDAOBase
    {
        #region Common methods
        public virtual ProductCat CreateProductCatFromReader(IDataReader reader)
        {
            ProductCat item = new ProductCat();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("ProCateID"))) item.ProCateID = (long)reader["ProCateID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Name"))) item.Name = (string)reader["Name"];
                if (!reader.IsDBNull(reader.GetOrdinal("Description"))) item.Description = (string)reader["Description"];
                if (!reader.IsDBNull(reader.GetOrdinal("SortOrder"))) item.SortOrder = (long)reader["SortOrder"];
                if (!reader.IsDBNull(reader.GetOrdinal("Enable"))) item.Enable = (bool)reader["Enable"];
                if (!reader.IsDBNull(reader.GetOrdinal("Parrentid"))) item.Parrentid = (long)reader["Parrentid"];
                if (!reader.IsDBNull(reader.GetOrdinal("CatLevel"))) item.CatLevel = (int)reader["CatLevel"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateProductCatFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateProductCat methods
            
        public virtual void CreateProductCat(ProductCat productCat)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spProductCatInsert");
                
                database.AddInParameter(dbCommand, "@Name", DbType.String, productCat.Name);
                database.AddInParameter(dbCommand, "@Description", DbType.String, productCat.Description);
                database.AddInParameter(dbCommand, "@SortOrder", DbType.Int64, productCat.SortOrder);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, productCat.Enable);
                database.AddInParameter(dbCommand, "@Parrentid", DbType.Int64, productCat.Parrentid);
                database.AddInParameter(dbCommand, "@CatLevel", DbType.Int32, productCat.CatLevel);
                database.AddOutParameter(dbCommand, "@ProCateID", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                productCat.ProCateID = (long)database.GetParameterValue(dbCommand, "@ProCateID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateProductCatException, ex);
            }
        }

        #endregion

        #region UpdateProductCat methods
        
        public virtual void UpdateProductCat(ProductCat productCat)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spProductCatUpdate");            
                
                database.AddInParameter(dbCommand, "@ProCateID", DbType.Int64, productCat.ProCateID);
                database.AddInParameter(dbCommand, "@Name", DbType.String, productCat.Name);
                database.AddInParameter(dbCommand, "@Description", DbType.String, productCat.Description);
                database.AddInParameter(dbCommand, "@SortOrder", DbType.Int64, productCat.SortOrder);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, productCat.Enable);
                database.AddInParameter(dbCommand, "@Parrentid", DbType.Int64, productCat.Parrentid);
                database.AddInParameter(dbCommand, "@CatLevel", DbType.Int32, productCat.CatLevel);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateProductCatException, ex);
            }
        }
        
        #endregion

        #region DeleteProductCat methods
        public virtual void DeleteProductCat(long proCateID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spProductCatDelete");
                
                database.AddInParameter(dbCommand, "@ProCateID", DbType.Int64, proCateID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteProductCatException, ex);
            }
        }

        #endregion

        #region GetProductCat methods
        
        public virtual ProductCat GetProductCat(long proCateID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spProductCatGet");
                
                database.AddInParameter(dbCommand, "@ProCateID", DbType.Int64, proCateID);
                
                ProductCat productCat =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        productCat = CreateProductCatFromReader(reader);
                        reader.Close();
                    }
                }
                return productCat;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetProductCatException, ex);
            }
        }
        
        #endregion

        #region GetProductCatList methods
        public virtual ProductCatCollection GetProductCatList(ProductCatColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spProductCatGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                ProductCatCollection productCatCollection = new ProductCatCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        ProductCat productCat = CreateProductCatFromReader(reader);
                        productCatCollection.Add(productCat);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return productCatCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetProductCatListException, ex);
            }
        }
        
        public virtual ProductCatCollection GetProductCatList(ProductCatColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetProductCatList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetProductCatList methods
        public virtual ProductCatCollection GetProductCatListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spProductCatGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                ProductCatCollection productCatCollection = new ProductCatCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        ProductCat productCat = CreateProductCatFromReader(reader);
                        productCatCollection.Add(productCat);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return productCatCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetProductCatListException, ex);
            }
        }
        
        public virtual ProductCatCollection GetProductCatListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetProductCatListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}